﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using IFRS.Models;

namespace IFRS.DataAccess.DAL
{
    public class SPR_COSTOFFUND
    {
        public DataTable SEARCH_ALL(CostOfFund costOfFund, Paging paging, bool isAllBranch, string sCostCenter, ref int iRecordCount)
        {
            DataTable dt;
            var utility = new IFRS_Utility();
            var lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@RecordCount", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;
                lstParams.Add(new SqlParameter("@PageIndex", paging.PageIndex.AsDBValue()));
                lstParams.Add(new SqlParameter("@PageSize", paging.PageSize.AsDBValue()));
                lstParams.Add(new SqlParameter("@Sorting", paging.Sorting.AsDBValue()));

                lstParams.Add(new SqlParameter("@isAllBranch", isAllBranch.AsDBValue()));
                lstParams.Add(new SqlParameter("@sCostCenter", sCostCenter.AsDBValue()));

                lstParams.Add(new SqlParameter("@TransactionDate", costOfFund.TransactionDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@IsAllColleteral", costOfFund.IsAllColleteral.AsDBValue()));
                lstParams.Add(new SqlParameter("@JudgementStatement", costOfFund.JudgementStatement.AsDBValue()));

                dt = utility.StoredProcDataTable("SPR_COSTOFFUND_SEARCHALL", lstParams.ToArray());

                iRecordCount = (int)lstParams[0].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public DataTable SEARCH_BYID(int costOfFundId)
        {
            DataTable dt;
            var utility = new IFRS_Utility();
            var lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@CostOfFundID", costOfFundId));
                dt = utility.StoredProcDataTable("SPR_COSTOFFUND_SEARCHBYID", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public int INSERT(CostOfFund costOfFund)
        {
            int iNum;
            var utility = new IFRS_Utility();
            var lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@CostOfFundID", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;

                lstParams.Add(new SqlParameter("@TransactionDate", costOfFund.TransactionDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@JudgementStatement", costOfFund.JudgementStatement.AsDBValue()));
                lstParams.Add(new SqlParameter("@IsAllColleteral", costOfFund.IsAllColleteral.AsDBValue()));
                lstParams.Add(new SqlParameter("@ColleteralTypeID", costOfFund.ColleteralTypeID.AsDBValue()));
                lstParams.Add(new SqlParameter("@CostOfFundValue", costOfFund.CostOfFundValue.AsDBValue()));

                lstParams.Add(new SqlParameter("@CreatedDate", costOfFund.CreatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedBy", costOfFund.CreatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedCostCenter", costOfFund.CreatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedBranch", costOfFund.CreatedBranch.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", costOfFund.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", costOfFund.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", costOfFund.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", costOfFund.UpdatedBranch.AsDBValue()));

                utility.ExecuteStoredProc("SPR_COSTOFFUND_INSERT", lstParams.ToArray());

                iNum = (int)lstParams[0].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int UPDATE(CostOfFund costOfFund)
        {
            int iNum;
            var utility = new IFRS_Utility();
            var lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@CostOfFundID", costOfFund.CostOfFundID.AsDBValue()));

                lstParams.Add(new SqlParameter("@TransactionDate", costOfFund.TransactionDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@JudgementStatement", costOfFund.JudgementStatement.AsDBValue()));
                lstParams.Add(new SqlParameter("@IsAllColleteral", costOfFund.IsAllColleteral.AsDBValue()));
                lstParams.Add(new SqlParameter("@ColleteralTypeID", costOfFund.ColleteralTypeID.AsDBValue()));
                lstParams.Add(new SqlParameter("@CostOfFundValue", costOfFund.CostOfFundValue.AsDBValue()));

                lstParams.Add(new SqlParameter("@UpdatedDate", costOfFund.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", costOfFund.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", costOfFund.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", costOfFund.UpdatedBranch.AsDBValue()));

                iNum = (int)utility.ExecuteStoredProc("SPR_COSTOFFUND_UPDATE", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int DELETE(CostOfFund costOfFund)
        {
            int iNum;
            var utility = new IFRS_Utility();
            var lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@CostOfFundID", costOfFund.CostOfFundID.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", costOfFund.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", costOfFund.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", costOfFund.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", costOfFund.UpdatedBranch.AsDBValue()));

                iNum = (int)utility.ExecuteStoredProc("SPR_COSTOFFUND_DELETE", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }
    }
}
